
// function to convert time
var convertTime = function(elapsedTime)
{
  // convert elapsed time to hour, min and seconds
  if (parseInt(elapsedTime) > 3600) 
  {
	elapsedTime = (Math.floor(parseInt(elapsedTime)/3600)).toString() +"hour " + (Math.floor(parseInt(elapsedTime)/60)).toString() +"min " + (parseInt(elapsedTime)%60).toString() + "s";
  }
  else if (parseInt(elapsedTime) > 60)
  {
	elapsedTime = (Math.floor(parseInt(elapsedTime)/60)).toString() +"min " + (parseInt(elapsedTime)%60).toString() + "s";
  }
  else
  {
	elapsedTime = elapsedTime+"s";
  }
  return elapsedTime;
}

// function to convert numbers to format with commas
function numberWithCommas(x)
{
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

// function to round to two digits, if necessary
function roundToTwo(num)
{    
  return +(Math.round(num + "e+2")  + "e-2");
}

window.onload = function ()
{
  // initialize parse
  Parse.initialize("EL9rGBARwKQl3NL4HAQ2U4GxQYjXV8dDDvDDaTPe", "WQLflsIP1zoVHCZgNjpr95dtrmaqjCaCkWSbOSrU");

  // get the user info
  var currentUser = Parse.User.current();
  
  // check if the user is signed in
  if (currentUser)
  {
	// display users name at top of page
	var username = currentUser.get("username");
	document.getElementById("statisticsHeader").innerHTML="Statistics for "+currentUser.get("username")+":";  
  }
  else
  {
	// tell the user they must login
	document.getElementById("content").innerHTML="You must login to view this page. </br> </br> <b> To login click <a href=\"login.html\">Here</a> <b>";

  }

  // LOAD GAME SCORES
  var GameScore = Parse.Object.extend("GameScore");
  var gameScore = new GameScore();
  
  // track totals
  var totalTimePlayed=0;
  var totalScore=0;
  var totalStagesCompleted=0;
  var totalYellowCoinsCollected=0;
  var totalRedCoinsCollected=0;
  var totalGreenCoinsCollected=0;
  var totalBlueCoinsCollected=0;
  var totalBoostsCollected=0;
  var totalMultipliersCollected=0;
  var totalHourglassesCollected=0;
  var totalFiresCollected=0;
  var totalPlays=0;
  
  var output = "<table> <tr> <th> Place </th> <th> Name </th> <th> Score </th> <th> Stage</th> <th> Time </th>  <th bgcolor><img src=\"assets/images/coinYellow.png\"></th> <th><img src=\"assets/images/coinRed.png\"></th> <th><img src=\"assets/images/coinGreen.png\"></th> <th><img src=\"assets/images/coinBlue.png\"></th> <th><img src=\"assets/images/boots.png\"></th> <th> <img src=\"assets/images/hourglass.png\"></th> <th><img src=\"assets/images/multiplier.png\"></th> <th><img src=\"assets/images/flame.png\"></th> </tr>"
  
  var GameScore = Parse.Object.extend("GameScore");
  var query = new Parse.Query(GameScore);
  query.equalTo("playerName",username);
  query.descending("score");
  query.find({
  success: function(results)
  {
	// Do something with the returned Parse.Object values
	totalPlays=results.length;
	document.getElementById('statisticsHeader').innerHTML="<b>"+currentUser.get("username")+": "+totalPlays+" attempts</b>"
	for (var i = 0; i < results.length; i++)
	{ 
	  var object = results[i];
	  var name = object.get('playerName').toString();
	  var score = object.get('score').toString();
	  var stagesComplete = object.get('stage').toString();
	  var elapsedTime = object.get('elapsedTime').toString();
	  var yellowCoinsCollected = object.get('yellowCoinsCollected').toString();
	  var redCoinsCollected = object.get('redCoinsCollected').toString();
	  var greenCoinsCollected = object.get('greenCoinsCollected').toString();
	  var blueCoinsCollected =  object.get('blueCoinsCollected').toString();
	  var multipliersCollected =  object.get('multipliersCollected').toString();
	  var clocksCollected =  object.get('clocksCollected').toString();
	  var bootsCollected =  object.get('bootsCollected').toString();
	  var firesCollected = object.get('firesCollected').toString();
	  
	  //update totals
	  totalTimePlayed+=parseInt(elapsedTime);
	  totalScore+=parseInt(score);
	  totalStagesCompleted+=parseInt(stagesComplete);
	  totalYellowCoinsCollected+=parseInt(yellowCoinsCollected);
	  totalRedCoinsCollected+=parseInt(redCoinsCollected);
	  totalGreenCoinsCollected+=parseInt(greenCoinsCollected);
	  totalBlueCoinsCollected+=parseInt(blueCoinsCollected);
	  totalBoostsCollected+=parseInt(bootsCollected);
	  totalMultipliersCollected+=parseInt(multipliersCollected);
	  totalHourglassesCollected+=parseInt(clocksCollected);
	  totalFiresCollected+=parseInt(firesCollected);
	  
	  //put commas
	  score = numberWithCommas(score);
	  stagesComplete = numberWithCommas(stagesComplete);
	  yellowCoinsCollected = numberWithCommas(yellowCoinsCollected);
	  redCoinsCollected = numberWithCommas(redCoinsCollected);
	  greenCoinsCollected = numberWithCommas(greenCoinsCollected);
	  blueCoinsCollected =  numberWithCommas(blueCoinsCollected);
	  multipliersCollected =  numberWithCommas(multipliersCollected);
	  clocksCollected =  numberWithCommas(clocksCollected);
	  bootsCollected =  numberWithCommas(bootsCollected);
	  firesCollected = numberWithCommas(firesCollected);
	  
	  //update table
	  if (i==0)
	  {
		output = output + "<tr bgcolor=#CC9900> <td>" + parseInt(i+1) +"</td> <td>" + name + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + "</td></tr>";
	  }
	  else if (i==1)
	  {
		output = output + "<tr bgcolor=#B8B894> <td>" + parseInt(i+1) +"</td> <td>" + name + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + "</td></tr>";
	  }
	  else if (i==2)
	  {
		output = output + "<tr bgcolor=#FF9900> <td>" + parseInt(i+1) +"</td> <td>" + name + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + " </td></tr>";
	  }
	  else
	  {
		output = output + "<tr> <td>" + parseInt(i+1) +"</td> <td>" + name + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + " </td> </tr>";
	  }
	
	}
	
	output = output + "</table>";
	//update table
	document.getElementById("scoreTable").innerHTML=output;
	//update totals
	document.getElementById("tScore").innerHTML=numberWithCommas(totalScore); 
	document.getElementById("tTime").innerHTML=convertTime(totalTimePlayed);
	document.getElementById("tStage").innerHTML=numberWithCommas(totalStagesCompleted);
	document.getElementById("tYellow").innerHTML=numberWithCommas(totalYellowCoinsCollected);
	document.getElementById("tRed").innerHTML=numberWithCommas(totalRedCoinsCollected);
	document.getElementById("tGreen").innerHTML=numberWithCommas(totalGreenCoinsCollected);
	document.getElementById("tBlue").innerHTML=numberWithCommas(totalBlueCoinsCollected);
	document.getElementById("tBoosts").innerHTML=numberWithCommas(totalBoostsCollected);
	document.getElementById("tClocks").innerHTML=numberWithCommas(totalHourglassesCollected);
	document.getElementById("tMultipliers").innerHTML=numberWithCommas(totalMultipliersCollected);
	document.getElementById("tFires").innerHTML=numberWithCommas(totalFiresCollected);
	  
	if (totalPlays> 0)
	{
	  document.getElementById("aScore").innerHTML=numberWithCommas(roundToTwo(totalScore/totalPlays));
	  document.getElementById("aTime").innerHTML=convertTime(roundToTwo(totalTimePlayed/totalPlays));
	  document.getElementById("aStage").innerHTML=numberWithCommas(roundToTwo(totalStagesCompleted/totalPlays));
	  document.getElementById("aYellow").innerHTML=numberWithCommas(roundToTwo(totalYellowCoinsCollected/totalPlays));
	  document.getElementById("aRed").innerHTML=numberWithCommas(roundToTwo(totalRedCoinsCollected/totalPlays));
	  document.getElementById("aGreen").innerHTML=numberWithCommas(roundToTwo(totalGreenCoinsCollected/totalPlays));
	  document.getElementById("aBlue").innerHTML=numberWithCommas(roundToTwo(totalBlueCoinsCollected/totalPlays));
	  document.getElementById("aBoosts").innerHTML=numberWithCommas(roundToTwo(totalBoostsCollected/totalPlays));
	  document.getElementById("aClocks").innerHTML=numberWithCommas(roundToTwo(totalHourglassesCollected/totalPlays));
	  document.getElementById("aMultipliers").innerHTML=numberWithCommas(roundToTwo(totalMultipliersCollected/totalPlays));
	  document.getElementById("aFires").innerHTML=numberWithCommas(roundToTwo(totalFiresCollected/totalPlays));
	}
	else
	{
	  document.getElementById("aTime").innerHTML=0;
	  document.getElementById("aScore").innerHTML=0;
	  document.getElementById("aStage").innerHTML=0;
	  document.getElementById("aYellow").innerHTML=0;
	  document.getElementById("aRed").innerHTML=0;
	  document.getElementById("aGreen").innerHTML=0;
	  document.getElementById("aBlue").innerHTML=0;
	  document.getElementById("aBoosts").innerHTML=0;
	  document.getElementById("aClocks").innerHTML=0;
	  document.getElementById("aMultipliers").innerHTML=0;
	  document.getElementById("aFires").innerHTML=0;
	}

  },
  error: function(error)
  {
	alert("Error: " + error.code + " " + error.message);
  }
  });
}
